package zl.com.gz_2017029e;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button loginBtn;
    private Toolbar toolbar;
    private LinearLayout logoLayout;
    private ImageView guideImg;
    private boolean isFirstStart;
    private int recLen=3;
    private int delay=2;
    private CheckBox rememberCB,autologoCB;
    private EditText logonAccount;
    private EditText logonPassword;
    private SharedPreferences pref;
    private boolean isRemember=false,isAutoLogin=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_home);
        }
        initView();
    }

    private void initView(){
        pref = getSharedPreferences("Remember",MODE_PRIVATE);
        logonAccount = (EditText) findViewById(R.id.login_account);
        logonPassword = (EditText) findViewById(R.id.login_password);
        String account=pref.getString("account","");
        String password = pref.getString("password","");
        logonAccount.setText(account);
        logonPassword.setText(password);
        rememberCB = (CheckBox)findViewById(R.id.remember_cb);
        autologoCB = (CheckBox)findViewById(R.id.atuologo_cb);
        autologoCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(autologoCB.isChecked()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            rememberCB.setChecked(true);
                        }
                    });
                }
            }
        });
        isRemember = pref.getBoolean("isRemember",false);
        isAutoLogin = pref.getBoolean("isAutoLogin",false);
        autologoCB.setChecked(isAutoLogin);
        if(autologoCB.isChecked()){
            rememberCB.setChecked(true);
        }else {
            rememberCB.setChecked(isRemember);
        }
        if (isAutoLogin){
            final Timer timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            delay--;
                            Toast.makeText(LoginActivity.this,"自动登录中....",Toast.LENGTH_SHORT).show();
                            if (delay<0){
                                loginBtn.performClick();
                                timer.cancel();
                            }
                        }
                    });
                }
            };
            timer.schedule(timerTask,2000,1000);
        }
        loginBtn = (Button)findViewById(R.id.login);
        loginBtn.setOnClickListener(this);
        //是否是第一次启动界面
        logoLayout = (LinearLayout)findViewById(R.id.logo_layout);
        guideImg = (ImageView)findViewById(R.id.login_guide_img);
        SharedPreferences pref= getSharedPreferences("isFirstStartActivity",MODE_PRIVATE);
        isFirstStart=pref.getBoolean("isFirst",true);
        isFirstStart();
    }

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.back:
                Toast.makeText(LoginActivity.this,"menu onclick",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(LoginActivity.this,"menu onclick",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(LoginActivity.this,"menu onclick",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    //判断是否是第一次启动程序
    private void isFirstStart(){
        if(isFirstStart){
            final Timer timer = new Timer();
            final SharedPreferences.Editor editor = getSharedPreferences("isFirstStartActivity",MODE_PRIVATE).edit();
//            editor.putBoolean("isFirst",false);
//            editor.apply();
//            Log.d("第一次启动后", "onCreate: is first start"+isFirstStart);
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    recLen--;
                    if(recLen<0){
                        editor.putBoolean("isFirst",false);
                        editor.apply();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                logoLayout.setVisibility(View.VISIBLE);
                                guideImg.setVisibility(View.GONE);
                                timer.cancel();
                            }
                        });
                    }
                }
            };
            timer.schedule(timerTask,0,1000);
        }else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    logoLayout.setVisibility(View.VISIBLE);
                    guideImg.setVisibility(View.GONE);
                }
            });
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login:
                String account=logonAccount.getText().toString().trim();
                String password=logonPassword.getText().toString().trim();
                if(account.equals("admin") && password.equals("123456")){
                    if (rememberCB.isChecked()){
                        SharedPreferences.Editor editor = getSharedPreferences("Remember",MODE_PRIVATE).edit();
                        editor.putBoolean("isRemember",true);
                        if (autologoCB.isChecked()){
                            editor.putBoolean("isAutoLogin",true);
                        }else {
                            editor.putBoolean("isAutoLogin",false);
                        }
                        editor.putString("account",account);
                        editor.putString("password",password);
                        editor.apply();
                    }
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }
}
